﻿'Proyecto de curso base de datos II
'Universidad Nacional de Ingeniería - Instituto de Estudios Superiories
'Copyright (c) 2008 - Adolfo Fitoria <adolfo.fitoria@gmail.com>
'Bajo licencia GNU Public Licence v.3 

Imports System.Data.SqlClient
Imports System.Data
Public Class Usuario

#Region "members"
    Private username As String
    Private _password As String
    Private usertype As String 'usertype pueden ser admin y usuario
    Private fecha_registro As Date
    Private ultimo_login As Date
    Private _primerNombre As String
    Private _segundoNombre As String
    Private _segundoApellido As String
    Private _primerApellido As String
    Private _email As String
    Private con As Conexion
#End Region
#Region "propiedades"
    Public ReadOnly Property tipoDeUsuario() As String
        Get
            Return usertype
        End Get
    End Property
    Public ReadOnly Property nombreUsuario() As String
        Get
            Return username
        End Get
    End Property
    Public ReadOnly Property NombreCompleto() As String
        Get
            Return Me._primerNombre + " " + Me._primerApellido
        End Get
    End Property

    Public ReadOnly Property FechaRegistro() As Date
        Get
            Return Me.fecha_registro
        End Get
    End Property

    Public ReadOnly Property FechaUltimoLogin() As Date
        Get
            Return Me.ultimo_login
        End Get
    End Property
    Public ReadOnly Property Email() As String
        Get
            Return _email
        End Get
    End Property
    Public ReadOnly Property primerNombre() As String
        Get
            Return _primerNombre
        End Get
    End Property
    Public ReadOnly Property segundoNombre() As String
        Get
            Return _segundoNombre
        End Get
    End Property
    Public ReadOnly Property primerApellido() As String
        Get
            Return _primerApellido
        End Get
    End Property
    Public ReadOnly Property segundoApellido() As String
        Get
            Return _segundoApellido
        End Get
    End Property
    Public ReadOnly Property Contrasena() As String
        Get
            Return _password
        End Get
    End Property
#End Region
#Region "Methods"
    Public Sub New(ByVal username As String, ByVal password As String)
        Me.username = username
        Me._password = password
    End Sub

    Public Sub New(ByVal username As String)
        Me.username = username
        Dim adapter As SqlClient.SqlDataAdapter
        Dim ds As DataSet = New DataSet
        con = New Conexion
        con.Conectar()
        Dim comando As SqlClient.SqlCommand = New SqlClient.SqlCommand("obtener_usuario", con.SqlCon)

        comando.CommandType = CommandType.StoredProcedure
        comando.Parameters.AddWithValue("@username", Me.username)
        If con.SqlCon.State = ConnectionState.Open Then
            adapter = New SqlClient.SqlDataAdapter(comando)
            adapter.Fill(ds)

            con.Desconectar()

            If ds.Tables(0).Rows.Count > 0 Then
                Me._password = ds.Tables(0).Rows(0).Item(1).ToString()
                Me.fecha_registro = Date.Parse(ds.Tables(0).Rows(0).Item(2).ToString())
                Me.ultimo_login = Date.Parse(ds.Tables(0).Rows(0).Item(3).ToString())
                Me.usertype = ds.Tables(0).Rows(0).Item(4).ToString
                Me._primerNombre = ds.Tables(0).Rows(0).Item(5).ToString
                Me._primerApellido = ds.Tables(0).Rows(0).Item(6).ToString
                Me._segundoNombre = ds.Tables(0).Rows(0).Item(7).ToString
                Me._segundoApellido = ds.Tables(0).Rows(0).Item(8).ToString
                Me._email = ds.Tables(0).Rows(0).Item(9).ToString
            Else

            End If
        End If
    End Sub
    Public Sub New(ByVal username As String, ByVal password As String, ByVal primerNombre As String, _
                    ByVal primerApellido As String, ByVal usertype As String, _
                    Optional ByVal segundoNombre As String = Nothing, _
                   Optional ByVal segundoApellido As String = Nothing, _
                   Optional ByVal email As String = Nothing)
        Me.username = username
        Me._password = password
        Me._primerApellido = primerApellido
        Me._primerNombre = primerNombre
        Me._segundoApellido = segundoApellido
        Me._segundoNombre = segundoNombre
        Me._email = email
        Me.usertype = usertype

        con = New Conexion()
        con.Conectar()
        Dim comando As New SqlCommand("crear_usuario", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure
        '       create procedure crear_usuario(@username nvarchar(20), @password nvarchar(20),
        '@primer_nombre nvarchar(20), @segundo_nombre nvarchar(20) = null,
        '@primer_apellido nvarchar(20), @segundo_apellido nvarchar(20) = null,
        '@email nvarchar(50)= null, @usertype varchar(20))
        comando.Parameters.AddWithValue("@username", Me.username)
        comando.Parameters.AddWithValue("@password", Me._password)
        comando.Parameters.AddWithValue("@primer_nombre", Me._primerNombre)
        comando.Parameters.AddWithValue("@segundo_nombre", Me._segundoNombre)
        comando.Parameters.AddWithValue("@primer_apellido", Me._primerApellido)
        comando.Parameters.AddWithValue("@segundo_apellido", Me._segundoApellido)
        comando.Parameters.AddWithValue("@email", Me._email)
        comando.Parameters.AddWithValue("@usertype", Me.usertype)

        comando.ExecuteNonQuery()
        con.Desconectar()
    End Sub

    Public Function Login() As Boolean
        Dim adapter As SqlClient.SqlDataAdapter
        Dim ds As DataSet = New DataSet
        con = New Conexion
        con.Conectar()
        Dim comando As SqlClient.SqlCommand = New SqlClient.SqlCommand("loguearse", con.SqlCon)

        comando.CommandType = CommandType.StoredProcedure
        comando.Parameters.AddWithValue("@username", Me.username)
        comando.Parameters.AddWithValue("@password", Me._password)
        If con.SqlCon.State = ConnectionState.Open Then
            adapter = New SqlClient.SqlDataAdapter(comando)
            adapter.Fill(ds)

            con.Desconectar()

            If ds.Tables(0).Rows.Count > 0 Then
                Me.fecha_registro = Date.Parse(ds.Tables(0).Rows(0).Item(2).ToString())
                Me.ultimo_login = Date.Parse(ds.Tables(0).Rows(0).Item(3).ToString())
                Me.usertype = ds.Tables(0).Rows(0).Item(4).ToString
                Me._primerNombre = ds.Tables(0).Rows(0).Item(5).ToString
                Me._primerApellido = ds.Tables(0).Rows(0).Item(6).ToString
                Me._segundoNombre = ds.Tables(0).Rows(0).Item(7).ToString
                Me._segundoApellido = ds.Tables(0).Rows(0).Item(8).ToString
                Me._email = ds.Tables(0).Rows(0).Item(9).ToString
                Return True
            Else
                Return False
            End If
        End If

    End Function

    Public Sub logout()
        con = New Conexion
        con.Conectar()
        Dim comando As New SqlClient.SqlCommand("logout", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure
        comando.Parameters.AddWithValue("@username", Me.username)
        comando.ExecuteNonQuery()
        con.Desconectar()
        Me.Finalize()
    End Sub

    Public Sub modificar(ByVal password As String, ByVal primerNombre As String, _
                    ByVal primerApellido As String, ByVal usertype As String, _
                    Optional ByVal segundoNombre As String = Nothing, _
                   Optional ByVal segundoApellido As String = Nothing, _
                   Optional ByVal email As String = Nothing)
        Me._password = password
        Me._primerApellido = primerApellido
        Me._primerNombre = primerNombre
        Me._segundoApellido = segundoApellido
        Me._segundoNombre = segundoNombre
        Me._email = email
        Me.usertype = usertype

        con = New Conexion()
        con.Conectar()
        Dim comando As New SqlCommand("modificar_usuario", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure

        comando.Parameters.AddWithValue("@username", Me.username)
        comando.Parameters.AddWithValue("@password", Me._password)
        comando.Parameters.AddWithValue("@primer_nombre", Me._primerNombre)
        comando.Parameters.AddWithValue("@segundo_nombre", Me._segundoNombre)
        comando.Parameters.AddWithValue("@primer_apellido", Me._primerApellido)
        comando.Parameters.AddWithValue("@segundo_apellido", Me._segundoApellido)
        comando.Parameters.AddWithValue("@email", Me._email)
        comando.Parameters.AddWithValue("@usertype", Me.usertype)

        comando.ExecuteNonQuery()
        con.Desconectar()
    End Sub

    Public Sub eliminar()
        con = New Conexion
        con.Conectar()
        Dim comando As New SqlClient.SqlCommand("eliminar_usuario", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure
        comando.Parameters.AddWithValue("@username", Me.username)
        comando.ExecuteNonQuery()
        con.Desconectar()
    End Sub
#End Region
End Class

    
